package com.bw.shopping.mvvm.viewmodel;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.entity.AddressEntity;
import com.bw.shopping.mvvm.model.ShipAddressModel;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public

/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/29 0029 下午 6:53
 */
class ShipAddressViewModel extends BaseViewModel<ShipAddressModel> {
    public ObservableField<String> name  = new ObservableField<>();
    public ObservableField<String> phone  = new ObservableField<>();
    public ObservableField<String> address  = new ObservableField<>();
    int user_id;
    @Override
    public void onCreate() {
        super.onCreate();


    }
    public void requestAdd(){
        user_id = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list().get(0).getUser_id();
        if (TextUtils.isEmpty(name.get())||TextUtils.isEmpty(phone.get())||TextUtils.isEmpty(address.get())){
         ToastUtils.showLong("不能为空");
        }else {
            HashMap<String,Object> map = new HashMap<>();
            map.put("shipAddress",address.get());
            map.put("shipIsDefault",1);
            map.put("shipUserMobile",phone.get());
            map.put("shipUserName",name.get());
            map.put("userId",user_id);
            m.requestAddress(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof AddressEntity){
            AddressEntity addressEntity = (AddressEntity) entity;
            if (addressEntity.getMessage().equals("添加地址成功")){
                ToastUtils.showLong("添加地址成功");
                finishPage();
            }else {
                ToastUtils.showLong("添加地址失败");
            }
        }
    }
    public void finish(){
        finishPage();
    }
    //保存
    public void save(){
        requestAdd();
    }


}
